package dongshi.daddy.raft.logcopy.statemachine;

public class Locker {
    // 打开密码箱指令
    public static final String COMMAND_OPEN = "open";
    // 关闭密码箱指令
    public static final String COMMAND_CLOSE = "close";
    // 密码箱默认状态是关闭
    private String currentState;
    private String pwd;

    // 用户指定密码柜密码
    public Locker(String pwd) {
        this.pwd = pwd;
        this.currentState = COMMAND_CLOSE;
        System.out.println("密码柜创造完毕，默认状态：" + this.currentState + ", 默认密码：" + this.pwd);
    }

    public void action(Action action) {
        String command = action.getCommand();
        System.out.println("收到命令：" + command);
        switch (command) {
            case COMMAND_OPEN:
                // 判断transition condition转移条件是否正确，这里就是判断密码是否正确，正确则打开密码柜
                if (this.pwd.equals(action.getPwd())) {
                    String oldState = currentState;
                    currentState = COMMAND_OPEN;
                    System.out.println("密码正确，打开密码柜，密码柜从状态【" + oldState + "】变为状态【" + currentState + "】");
                } else {
                    System.out.println("密码错误，无法打开密码柜！");
                }
//                break;
                return;
            case COMMAND_CLOSE:
                String oldState = currentState;
                currentState = COMMAND_CLOSE;
                System.out.println("关闭密码柜，密码柜从状态【" + oldState + "】变为状态【" + currentState + "】");
//                break;
                return;
            default:
                System.out.println("错误的指令，支持指令列表：。。。");
//                break;
                return;
        }
    }

    public Locker() {}

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
}
